package com.oa.business.org;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.business.org.dto.DutyDto;
import com.oa.common.result.Result;
import com.oa.framework.org.domain.Duty;
import com.oa.framework.org.service.DutyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("duty")
public class DutyController {

    @Autowired
    private DutyService dutyService;

    @GetMapping("search")
    public Result search(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        Page<Duty> pageWrapper = new Page<>(page, limit);
        IPage<Duty> duties = dutyService.search(pageWrapper);
        return Result.paginationResult(duties.getPages(), duties.getSize(), duties.getTotal(), duties.getRecords());
    }

    @GetMapping("info/{id}")
    public Result info(@PathVariable Long id){
        return Result.success(dutyService.getDutyDetail(id));
    }

    @PostMapping("add")
    public Result add(@RequestBody DutyDto dutyDto){
        return Result.success(dutyService.addDuty(dutyDto.transform()));
    }

    @PostMapping("edit")
    public Result edit(@RequestBody DutyDto dutyDto){
        return Result.success(dutyService.updateDuty(dutyDto.transform()));
    }

    @PostMapping("disable/{id}")
    public Result disable(@PathVariable Long id){
        return Result.success(dutyService.disable(id));
    }
}
